<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/12/012
 * Time: 14:19
 */

namespace app\project\controller;


use app\common\controller\Common;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Plan extends Controller
{
    private $pro_id = 0;
    public function _initialize()
    {
        $swidthStyle = Session::get("style");
        $this->assign("swidthStyle",$swidthStyle);
        $pro_id = empty($_GET['pro_id'])?0: $_GET['pro_id'];
        $this->assign("pro_id",$pro_id);

        $library = empty($_GET['library'])?0:$_GET['library'];
        $this->assign("library",$library);
    }

    /**
     * 年度计划进度
     */
    public function index(){

        $common = new Common();
        //查询项目
        $title = $common->public_select("project_pro",['id'=>$_GET['pro_id']])->value('title');
        $this->assign("title",$title);

        if(!empty($_GET['keyword'])){
            $keyword = $_GET['keyword'];
            $where['title'] = ['like',"%{$keyword}%"];
        }
        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $where['pro_id'] = $_GET['pro_id'];
                $list = $common->public_select("project_pro_plan",$where,null,$start,$limit)->order("id desc")->select();
                $count = $common->public_select("project_pro_plan",$where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }

        return $this->fetch("index");
    }

    /**
     * 年度计划进度视图
     */
    public function detail(){
        $common = new Common();
        if(!empty($_GET['id'])){
            $id = $_GET['id'];
            $detail = $common->public_select("project_pro_plan",['id'=>$id])->find();
//            $detail['images'] = explode(";",$detail['images']);
            $this->assign('detail',$detail);
        }
        $template = empty($_GET['id'])?"addView":"saveView";
        return $this->fetch($template);
    }


    /**
     * 添加
     * @return mixed
     */
    public function add(){
        $thisWechat = Session::get("thisWechat");
        $data = $this->request->post();
        $common = new Common();
        try{
            $this->condition($data);
//            if(!empty($data['images'])){
//                $data['images'] = implode(';',$data['images']);
//            }
            $data['addtime'] = date("Y-m-d h:i:s",time());
            $data['add_id'] = Session::get("id");
            $title = $common->public_select("project_pro",['id'=>$data['pro_id']])->value("title");
            $id = $common->handle("project_pro_plan",$data,1);
            $log['remark'] = "【工程管理】用户【" . Session::get("user_name") . "】添加项目【'.$title.'】年度计划进度,关联id【" . $id . "】";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 修改
     * @return mixed
     */
    public function save(){
        $thisWechat = Session::get("thisWechat");
        $data = $this->request->post();
        $common = new Common();
        try{
            $this->condition($data);
            if(!empty($data['images'])){
                $data['images'] = implode(';',$data['images']);
            }
            $data['updatetime'] = date("Y-m-d h:i:s",time());
            $data['update_id'] = Session::get("id");
            $title = $common->public_select("project_pro",['id'=>$data['pro_id']])->value("title");
            $common->handle("project_pro_plan",$data,1);
            $log['remark'] = "【工程管理】用户【" . Session::get("user_name") . "】更新项目【'.$title.'】年度计划进度,关联id【" . $data['id'] . "】";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 删除
     */
    public function del(){
        try{
            $common = new Common();
            $config = $common->selconfig();
            $id = $_POST['id'];
            $list = Db::connect($config)->name("project_pro_plan")->alias("a")
                ->join("project_pro b","a.pro_id = b.id")->where(['a.id'=>['in',$id]])
                ->field("b.title,a.id")->select();
            $res = $common->public_select("project_pro_plan",['id'=>['in',$id]])->delete();
            if(empty($res)){
                throw new Exception("删除失败");
            }
            foreach ($list as $k=>$v){
                $log['remark'] = "【工程管理】,账号【" . Session::get("user_name") . "】删除项目【".$v['title']."】年度计划进度,关联id【" . $v['id'] . "】";
                $common->addLog($log);
            }
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    /**
     * @param $data
     */
    public function condition($data){
        $rule = [
            ["plan_title", "require", "年份不能为空"],
            ["plan_content", "require", "重要工作节点推进计划不能为空"],
        ];
        $validate = new Validate($rule);
        if (!$validate->check($data)) {
            throw new Exception($validate->getError());
        }
    }
}